package com.design.interpreter;

/**
 * @author leali
 * @title: PrimitiveCommandNode
 * @projectName design
 * @description: 基本命令节点类：终结符表达式
 * @date 2022/1/3 19:02
 */
public class PrimitiveCommandNode extends Node {
    private String name;
    private String text;
    @Override
    public void interpret(Context context) {
        name = context.currentToken();
        context.skipToken(name);
        if (!"PRINT".equals(name) && !"BREAK".equals(name) && !"SPACE".equals (name)){
            System.err.println("非法命令！");
        }
        if ("PRINT".equals(name)){
            text = context.currentToken();
            context.nextToken();
        }
    }

    @Override
    public void execute() {
        if ("PRINT".equals(name)) {
            System.out.print(text);
        } else if ("SPACE".equals(name)) {
            System.out.print(" ");
        } else if ("BREAK".equals(name)) {
            System.out.println();
        }
    }
}
